package com.yuhuofei2021.spring.boot.weather.job;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.yuhuofei2021.spring.boot.weather.entity.City;
import com.yuhuofei2021.spring.boot.weather.service.WeatherDataCollectionService;
/**
 * 
 * @description 天气数据采集子服务，由定时任务调用外部接口，采集天气数据并存储到redis中
 * @author yuhuofei2021
 * @date 2021年10月3日
 */
public class WeatherDataSyncJob extends QuartzJobBean {

	private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);
	
	@Resource
	private WeatherDataCollectionService weatherDataCollectionService;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		logger.info("=============自动查询天气信息环节开始！===========");
		//获取城市id列表
		//TODO 改由城市数据API子服务来提供城市数据
		List<City> cityList = null;
		try {
			//TODO 改由城市数据API子服务来提供城市数据
			cityList = new ArrayList<>();
			City city = new City();
			city.setCityId("101280601");
			cityList.add(city);
		} catch (Exception e) {
			logger.error("获取城市列表出错！",e);
		}
		
		//遍历城市id获取天气
		for (City city : cityList) {
			String cityId = city.getCityId();
			logger.info("要查询天气的城市的id是：" + cityId);
			weatherDataCollectionService.syncDataByCityId(cityId);
		}
		
		logger.info("=============自动查询天气信息环节结束！===========");
	}

}
